<?php

namespace app\common\service;
use Dm\Request\V20151123 as Dm;

class MailService{

	// //国内
	public static function Mail($mail,$type=1){
		$digits = C('mail_code_digits');
		$code   = rand('1'.str_repeat(0,$digits-1),str_repeat(9,$digits));
		include_once '../extend/MAIL/ali/aliyun-php-sdk-core/Config.php';
	                
	    //需要设置对应的region名称，如华东1（杭州）设为cn-hangzhou，新加坡Region设为ap-southeast-1，澳洲Region设为ap-southeast-2。
	    $iClientProfile = \DefaultProfile::getProfile("cn-hangzhou", "LTAI4G8Mj7vqL6USRNvnfb9x", "9S6c6OQcAnxx3ReJIw0xi8JvBL3U59");        
	    //新加坡或澳洲region需要设置服务器地址，华东1（杭州）不需要设置。
	    //$iClientProfile::addEndpoint("ap-southeast-1","ap-southeast-1","Dm","dm.ap-southeast-1.aliyuncs.com");
	    //$iClientProfile::addEndpoint("ap-southeast-2","ap-southeast-2","Dm","dm.ap-southeast-2.aliyuncs.com");
	    $client = new \DefaultAcsClient($iClientProfile);    
	    $request = new Dm\SingleSendMailRequest();     
	    //新加坡或澳洲region需要设置SDK的版本，华东1（杭州）不需要设置。
	    //$request->setVersion("2017-06-22");
	    $request->setAccountName("service@mail.shellpro.me");
	    $request->setFromAlias("传奇");
	    $request->setAddressType(1);
	    $request->setTagName("控制台创建的标签");
	    $request->setReplyToAddress("true");
	    $request->setToAddress($mail);
	    //可以给多个收件人发送邮件，收件人之间用逗号分开,若调用模板批量发信建议使用BatchSendMailRequest方式
	    //$request->setToAddress("邮箱1,邮箱2");
	    $request->setSubject("验证邮件");
	    $request->setHtmlBody('<table style="min-width: 332px; max-width: 600px; border: 1px solid #F0F0F0; border-bottom: 1px solid #C0C0C0; border-top: 0; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px;" width="100%" cellspacing="0" cellpadding="0" border="0" bgcolor="#FAFAFA"><tbody><tr height="16px"><td rowspan="3" width="32px"></td><td></td><td rowspan="3" width="32px"></td></tr><tr><td><p>尊敬的用户：</p><p>我们收到了一项请求，要求通过您的电子邮件地址验证您的信息 <span style="color: #659CEF" dir="ltr"></span>。您的验证码为：</p><div style="text-align: center;"><p dir="ltr"><strong style="text-align: center; font-size: 24px; font-weight: bold;">'.$code.'</strong></p></div><p>如果您并未请求此验证码，则可能是他人正在尝试访问,请忽略该邮件。<strong>请勿将此验证码转发给或提供给任何人。</strong></p></td></tr><tr height="32px"></tr></tbody></table>');        
	    try {
	        $response = $client->getAcsResponse($request);
	        // print_r($response);
	    }
	    catch (ClientException  $e) {
	        // print_r($e->getErrorCode());   
	        // print_r($e->getErrorMessage());   
	        return ['code' => 1, 'msg' => $e->getErrorMessage()];
	    }
	    catch (ServerException  $e) {        
	        // print_r($e->getErrorCode());   
	        // print_r($e->getErrorMessage());
	        return ['code' => 1, 'msg' => $e->getErrorMessage()];
	    }
	    $re = model('MailCodeModel')->addCode($mail, $code, $type);
		if ($re) {
			return ['code' => 0, 'msg' => 'success'];
		} else {
			return ['code' => 1, 'msg' => 'fail'];
		}
	}
	// 
	// public static function mail($mail,$type=1){
	// 	$digits = C('mail_code_digits');
	// 	$code   = rand('1'.str_repeat(0,$digits-1),str_repeat(9,$digits));
	// 	require '../extend/SUBMAIL_PHP_SDK-master/app_config.php';
	// 	require_once '../extend/SUBMAIL_PHP_SDK-master/SUBMAILAutoload.php';
	// 	$submail=new \MAILSend($mail_configs);
	// 	$submail->AddTo($mail,"");
	// 	$submail->SetSender("service@mail.shellpro.me","");
	// 	$submail->SetSubject("验证邮件");
	// 	$submail->SetText("验证邮件");
	// 	$submail->SetHtml('<table style="min-width: 332px; max-width: 600px; border: 1px solid #F0F0F0; border-bottom: 1px solid #C0C0C0; border-top: 0; border-bottom-left-radius: 3px; border-bottom-right-radius: 3px;" width="100%" cellspacing="0" cellpadding="0" border="0" bgcolor="#FAFAFA"><tbody><tr height="16px"><td rowspan="3" width="32px"></td><td></td><td rowspan="3" width="32px"></td></tr><tr><td><p>尊敬的用户：</p><p>我们收到了一项请求，要求通过您的电子邮件地址验证您的信息 <span style="color: #659CEF" dir="ltr"></span>。您的验证码为：</p><div style="text-align: center;"><p dir="ltr"><strong style="text-align: center; font-size: 24px; font-weight: bold;">'.$code.'</strong></p></div><p>如果您并未请求此验证码，则可能是他人正在尝试访问,请忽略该邮件。<strong>请勿将此验证码转发给或提供给任何人。</strong></p></td></tr><tr height="32px"></tr></tbody></table>');
	// 	$res = $submail->send();
	// 	$res = json_decode($res,1);
	// 	if ($res['status'] != 'success') {
	// 		return ['code' => 1, 'msg' => 'fail'];
	// 	}
	//     $re = model('MailCodeModel')->addCode($mail, $code, $type);
	// 	if ($re) {
	// 		return ['code' => 0, 'msg' => 'success'];
	// 	} else {
	// 		return ['code' => 1, 'msg' => 'fail'];
	// 	}
	// }	
}